package com.c3w.synchro.member.dao;

import com.c3w.synchro.common.dao.BeanDao;
import com.c3w.synchro.member.bean.Athlete;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * User: magirard
 * Date: 12-11-13
 */
@Repository("athleteDao")
public class AthleteDaoImpl extends BeanDao<Athlete> implements AthleteDao {

    public AthleteDaoImpl() {
        super(Athlete.class);
    }

    public List<Athlete> findAll(Athlete athlete) {
        boolean baliseClause = false;
        StringBuilder stringBuilder = new StringBuilder("SELECT a FROM Athlete WHERE ");

        if (athlete.getClubName() != null) {
            stringBuilder.append(athlete.getClubName());
            baliseClause = true;
        }

        if (athlete.getCategory() != null) {
            if (baliseClause) {
                stringBuilder.append(" and ");
            }
            stringBuilder.append(athlete.getCategory());
        }

        Query query = em.createQuery(stringBuilder.toString());
        return query.getResultList();
    }

}

